#include "LPC11xx.h"
#include <stdbool.h>
#include "gpio.h"
#include "timer.h"
#include "utilio.h"
#include "ad9954.h"

/***************************************************************
 * Macro definitions                                           *
 ***************************************************************/
#define us2TimerInterval(us) ( us * (SystemAHBFrequency / 1000000) )
#define ms2TimerInterval(ms) ( ms * (SystemAHBFrequency / 1000) )

#define TIMER_DELAY     TIMER32[0]
#define TIMER_SWEEP     TIMER32[1]

/***************************************************************
 * Globol variables                                            *
 ***************************************************************/

/***************************************************************
 * Globol functions                                            *
 ***************************************************************/
extern void delayms(uint32_t us)
{
    TimerDelayMs(TIMER_DELAY, us);
}
extern void delayus(uint32_t us)
{
    TimerDelayUs(TIMER_DELAY, us);
}

/***************************************************************
 * Functions                                                   *
 ***************************************************************/
void changeDirection(void * unuse)
{
    static bool dir = 0;
    dir = !dir;
    ad9954_line_set(LINE_PS0, dir);
}

/***************************************************************
 * MAIN                                                        *
 ***************************************************************/
int main (void)
{
    uint32_t freq0= 0, freq1 = 0;
    
    SystemInit();             /* MUST BE INCLUDED! */

    GPIOInit();
    GPIOSetDir(PORT2, 7, GPIO_DIR_OUTPUT);

    // for delay
    TimerInit(TIMER_DELAY);
    
    // for sweep direction switching.
    TimerInit(TIMER_SWEEP);
    TimerSetIntCallback(TIMER_SWEEP, changeDirection);
    TimerSetTime(TIMER_SWEEP, ms2TimerInterval(500));
    TimerEnable(TIMER_SWEEP);
    
    MyStdin=KEY;
    MyStdout=LCD+ECHO;
    UtilioInit();
    
//    ad9954_init();
//    ad9954_set_clk(25000000, 8);
    
    while (1) {
        printf("Freq0 (KHz): ");
        scanf("%d", &freq0);
        printf("Freq1 (KHz): ");
        scanf("%d", &freq1);
//        ad9954_fsk_set(freq0 * 1000, freq1 * 1000);
        printf("OK!\r\n");
    }
}

/***************************************************************
 * END OF FILE                                                 *
 ***************************************************************/
